package ug.co.icom.kyazze.healthticket.services;

import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import ug.co.icom.kyazze.healthticket.DataBase.Database;
import ug.co.icom.kyazze.healthticket.data.QueryBuilder;
import android.os.AsyncTask;

public class PutAllergyData extends AsyncTask<String, Void, String> {

	@SuppressWarnings("unused")
	private Exception exception;
	Database query_handler;

	protected String doInBackground(String... params) {
		try {

			String documnet_id = params[0];
			QueryBuilder qb = new QueryBuilder();
			URL url = new URL(qb.buildMedicalObjectUrlPutURL(documnet_id));

			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestMethod("PUT");
			connection.setDoOutput(true);
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setRequestProperty("Accept", "application/json");

			String a_name = params[1];
			String a_date = params[2];
			String a_severity = params[3];
			String a_reaction = params[4];
			String a_occurence = params[5];
			String a_comments = params[6];

			OutputStreamWriter osw = new OutputStreamWriter(
					connection.getOutputStream());

			String query = String
					.format("{\"document\" : { \"$addToSet\" : {\"allergies\" : {\"name\" : \"%s\" , \"date_of_diagnosis\" : \"%s\", \"severity\" : \"%s\", \"reaction\" : \"%s\", \"occurence\" : \"%s\", \"additional_comments\" : \"%s\" }} }}",
							a_name, a_date, a_severity, a_reaction,
							a_occurence, a_comments);

			osw.write(query);
			osw.flush();
			osw.close();

			System.err.println(connection.getResponseCode());
		} catch (Exception e) {
			this.exception = e;
			return null;
		}
		return null;
	}

}
